package com.yf.modules.security.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author chentianwei
 * @date 2023/4/24 13:31
 * @description 鉴权切入点
 */
@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {

        //用户访问受保护的Rest资源时，抛出401异常
        httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED,e.getMessage());
    }
}
